<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      v-show="showSearch"
      label-width="80px"
      class="form-search"
    >
      <SearchTool
        :code="{
          factory: 'factoryCode',
        }"
        v-model:factory="queryParams.factoryCode"
        :showWorkshop="false"
        :showLine="false"
        :showSection="false"
      ></SearchTool>
      <el-form-item label="产品分类" prop="prodType">
        <el-select clearable filterable placeholder="请选择产品分类" v-model="queryParams.prodType">
          <el-option
            v-for="(item, index) in typeList"
            :key="index"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="产品编号" prop="prodNo">
        <el-input
          v-model="queryParams.prodNo"
          placeholder="请输入产品编号"
          clearable
          style="width: 240px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品名称" prop="prodName">
        <el-input
          v-model="queryParams.prodName"
          placeholder="请输入产品名称"
          clearable
          style="width: 240px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['tech:recipe:add']"
        >
          新增
        </el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button
          :loading="syncLoading"
          type="warning"
          plain
          icon="Refresh"
          @click="handleSync"
          v-hasPermi="['tech:recipe:sync']"
        >
          同步ERP数据
        </el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="tableData"
      show-overflow-tooltip
      border
      maxHeight="500"
      @selection-change="handleSelectionChange"
    >
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <el-table-column label="产品编号" width="140" align="center" prop="prodNo">
        <template #default="scope">
          <el-link type="primary" @click="handleDetail(scope.row)">{{ scope.row.prodNo }}</el-link>
        </template>
      </el-table-column>
      <el-table-column
        label="产品名称"
        align="center"
        prop="prodName"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="产品分类"
        align="center"
        prop="prodTypeName"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="工厂"
        align="center"
        prop="factoryName"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="标准生产数量"
        align="center"
        prop="standardProdCount"
        :show-overflow-tooltip="true"
      />
      <el-table-column label="单位" align="center" prop="unit" :show-overflow-tooltip="true" />
      <el-table-column label="规格" align="center" prop="packages" :show-overflow-tooltip="true" />
      <el-table-column label="型号" align="center" prop="model" :show-overflow-tooltip="true" />
      <el-table-column label="品牌" align="center" prop="brand" :show-overflow-tooltip="true" />
      <el-table-column label="版本号" align="center" prop="version" :show-overflow-tooltip="true" />
      <el-table-column
        label="修改时间"
        align="center"
        prop="updateTime"
        :show-overflow-tooltip="true"
      />
      <el-table-column label="来源" align="center" prop="source" :show-overflow-tooltip="true" />
      <!-- <el-table-column
        label="状态"
        align="center"
        prop="status"
        :show-overflow-tooltip="true"
      /> -->
      <el-table-column label="状态" prop="status">
        <template #default="scope">
          <dict-tag :options="tech_recipe_status" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        width="160"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button
            v-if="scope.row.source == 'MES'"
            link
            v-hasPermi="['tech:recipe:edit']"
            type="primary"
            icon="Edit"
            @click="handleUpdate(scope.row)"
          >
            编辑
          </el-button>
          <el-button
            v-if="scope.row.source == 'MES'"
            v-hasPermi="['tech:recipe:remove']"
            link
            type="primary"
            icon="Delete"
            @click="handleDelete(scope.row)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script setup name="Dict">
import { listRecipe, delBatchRecipe, syncErpRecipeBom } from "@/api/technology/formulation";
import { ref, reactive, toRefs, getCurrentInstance } from "vue";
import { PRODUCT, SF_PRODUCT } from "@/constant";
import detail from "./data";
const { proxy } = getCurrentInstance();
const { tech_recipe_status } = proxy.useDict("tech_recipe_status");
const tableData = ref([]);
const syncLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const dateRange = ref([]);

const data = reactive({
  form: {},
  typeList: [
    {
      label: "成品",
      value: PRODUCT,
    },
    {
      label: "半成品",
      value: SF_PRODUCT,
    },
  ],
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    name: undefined,
  },
  rules: {
    name: [{ required: true, message: "产品名称不能为空", trigger: "blur" }],
  },
});

const { queryParams, typeList } = toRefs(data);

/** 查询产品类型列表 */
function getList() {
  loading.value = true;
  listRecipe(proxy.addDateRange(queryParams.value, dateRange.value)).then((response) => {
    tableData.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

function handleSync() {
  syncLoading.value = true;
  syncErpRecipeBom()
    .then(() => {
      proxy.$modal.msgSuccess("同步成功");

      syncLoading.value = false;
      getList();
    })
    .catch(() => {
      syncLoading.value = false;
    });
}

/** 重置按钮操作 */
function resetQuery() {
  dateRange.value = [];
  proxy.resetForm("queryRef");
  handleQuery();
}

/** 新增按钮操作 */
function handleAdd() {
  proxy.subPage(detail, { title: "新增产品" }, () => {
    getList();
  });
}

/** 多选框选中数据 */
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

function handleDetail(row) {
  proxy.subPage(detail, { title: "查看产品详情", data: row }, () => {
    getList();
  });
}

/** 修改按钮操作 */
function handleUpdate(row) {
  proxy.subPage(detail, { title: "编辑产品信息", data: row }, () => {
    getList();
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const idList = row.id || ids.value.join(",");
  let msg = "是否确认删除所选的数据项？";
  if (row.prodNo) {
    msg = `是否确认删除产品编号为${row.prodNo}的数据项？`;
  }
  proxy.$modal
    .confirm(msg)
    .then(function () {
      return delBatchRecipe({ ids: idList });
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}

getList();
</script>
